<template>
  <div>
    <div class="search-container" @click="$router.push('/search')">
      <div class="search">
      <van-icon name="search" />
        <span>搜索商品</span>
      </div>
    </div>
    <div class="cate-container">
      <div class="cates-wrap">
        <div class="cates">
          <div :class="['cate', {
            active: index === activeIndex
          }]"
            @click="switchTab(index)"
            v-for="(cate, index) in cates" :key="cate.id">
            {{cate.name}}
          </div>
        </div>
      </div>
      <div class="items-wrap">
        <div class="items">
          <div class="item"
            @click="clickItem(item.id)"
            v-for="item in items" :key="item.id">
            <img :src="item.pic" class="item-img">
            <div class="item-info">
              <h4>{{item.name}}</h4>
              <div class="item-desc">
                {{item.characteristic? item.characteristic.length < 10 ?item.characteristic.length:item.characteristic.substr(0, 10)+'...' : '暂无描述'}}
              </div>
              <div class="item-action">
                <div class="price">
                  ￥{{item.minPrice}}
                </div>
              </div>
            </div>
          </div>
        </div>

      </div>
    </div>
    <my-tabbar/>
  </div>
</template>

<script>
import MyTabbar from '../../components/MyTabbar.vue'

import { fetchCates, fetchItems } from '_api'
export default {
  data () {
    return {
      cates: [], // 商品所有的分类
      items: [], // 分类对应的商品
      activeIndex: 0 // 高亮下标
    }
  },
  created () {
    this.fetchCates()
  },
  methods: {
    clickItem (id) {
      // 进入详情
      this.$router.push(`/detail/${id}`)
    },
    fetchItems () {
      // 请求分类对应商品列表
      fetchItems({
        categoryId: this.cates[this.activeIndex].id
      }).then(res => {
        if (res.data.code === 0) {
          this.items = res.data.data.result
        }
        if (res.data.code === 700) {
          this.items = []
        }
      })
    },
    switchTab (index) {
      // 切换高亮类
      this.activeIndex = index
      this.fetchItems()
    },
    // 请求所有分类
    fetchCates () {
      fetchCates().then(res => {
        if (res.data.code === 0) {
          this.cates = res.data.data
          this.fetchItems()
        }
      })
    }
  },
  components: { MyTabbar },
  name: 'Cate'
}
</script>

<style lang="scss" scoped>
.search-container{
  height: 2.65rem;
  border-bottom: 2px solid #eee;
  background-color: #fff;
  padding: 0 10px;
  display: flex;
  align-items: center;
  .search{
    height: 1.9rem;
    background-color: #F3F5F7;
    font-size: 14px;
    color: #666;
    display: inline-flex;
    align-items: center;
    width: 100%;
    border-radius: 0.45rem;
    .icon{
      margin: 0 10px;
    }
  }
  .van-icon-search{
    padding: 10px;
  }
}
.cate-container{
  position: fixed;
  left:0;
  right: 0;
  top: 38px;
  bottom: 50px;
  display: flex;
  background-color: #fff;
  .cates-wrap{
    width: 5rem;
    height: 100%;
    overflow-y: auto;
    border-right: 1px solid #999;
    .cate{
      font-size: 14px;
      color: #333;
      text-align: center;
      line-height: 5.25rem;
      position: relative;
      &.active{
        color: #ff734c;
        &:after{
          content: '';
          position: absolute;
          width: 2px;
          height: 4.9rem;
          background-color: #ff734c;
          left: 0;
          top: 0.175rem;
        }
      }
    }
  }
  .items-wrap{
    .items{
      margin: 10px;

    }
     .item{
    display: flex;
    background-color: #fff;
    margin-bottom: 10px;
    border-radius: 5px;
    overflow: hidden;
    font-size: 14px;
    overflow-y: auto;
    .item-img{
      width: 5rem;
      height: 5rem;
    }
    .item-info{
      flex: 1;
      padding-left: 10px;
      h4{
        margin: 0;
        font-size: 16px;
        color: #333;
        line-height: 30px;
      }
      .item-desc{
        font-size: 14px;
        color: #666;
        margin:5px 0;
      }
      .item-action {
        display: flex;
        justify-content: space-between;
        height: 1rem;
        padding-right: 10px;
        align-items: center;
        .price{
          font-size: 16px;
          font-weight: 900;
          color: #333;
        }
      }
    }
  }
  }
}
</style>
